<?php

namespace app\widgets;

use app\models\Banner;
use yii\base\Widget;
use yii\helpers\Url;

class AppBannerWidget extends Widget {

    public $template = '<div class="swiper-slide"><a href="javascript:;" data-href="{url}"><img src="{image}" width="100%"/></a></div>';

    public $type;

    public function run() {
        $model = new Banner();
        $banners = $model->getBanners($this->type);
        $items = [];
        foreach ($banners as $banner) {
            $url = empty($banner['url']) ? 'javascript:;' : $banner['url'];
            $image = $banner['image'];
            $items[] = strtr($this->template, [
                '{url}' => $url,
                '{image}' => Url::to($image, TRUE),
            ]);
        }
        return implode("\n", $items);
    }
}